<?php

namespace app\admin\model;

use think\Model;

class Gateway extends Model
{
    // 表名
    protected $name = 'gateway';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'state_text',
        'register_text',
        'last_call_time_text'
    ];
    

    
    public function getStateList()
    {
        return ['1' => __('State 1'),'0' => __('State 0')];
    }     

    public function getRegisterList()
    {
        return ['receive' => __('Register receive'),'send' => __('Register send'),'none' => __('Register none')];
        //return ['receive' => __('Register receive'),'send' => __('Register send')];
        //return ['receive' => __('Register receive')];
    }     


    public function getStateTextAttr($value, $data)
    {       
	$data['state'] = empty($data['state']) ? '' :  $data['state']; 
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRegisterTextAttr($value, $data)
    {       
	$data['register'] = empty($data['register']) ? '' :  $data['register']; 
        $value = $value ? $value : $data['register'];
        $list = $this->getRegisterList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLastCallTimeTextAttr($value, $data)
    {
	$data['last_call_time'] = empty($data['last_call_time']) ? '' :  $data['last_call_time'];
        $value = $value ? $value : $data['last_call_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setLastCallTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
